
using System;

namespace TypingMonkey
{
	public class Typewriter
	{
		public Paper paper;
		public Keyboard keyboard;
		public long counter;
		public Target target;

		public Typewriter(string str)
		{
			this.paper = new Paper();
			this.keyboard = new Keyboard();
			this.keyboard.SubscribeButtonPressedEvent(new ButtonPressedEventHandler(PrintNumber));
			this.target = new Target(str);
		}

		public class Keyboard
		{
			public char[] buttons;
			private int bPressed;
			public event ButtonPressedEventHandler ButtonPressedEvent;
			private Random rnd;

			public Keyboard()
			{
				this.buttons = new char[] {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',// '[', ']', '\\',
							'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',// ';', '\'',
							'z', 'x', 'c', 'v', 'b', 'n', 'm',// ',', '.', '/',
//							'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '|',
//							'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
//							'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
//							'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
//							'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
							' ', '\n'//, '\t', '\b'
							};
							this.rnd = new Random();
				 
			}

			public void PressButton()
			{
				bPressed = rnd.Next(0, this.buttons.Length);
				OnButtonPressedEvent(bPressed);
			}

			private void OnButtonPressedEvent(int num)
			{
					ButtonPressedEvent(new ButtonPressedEventArgs(num));
			}

			public void SubscribeButtonPressedEvent(ButtonPressedEventHandler eventHandler)
			{
				ButtonPressedEvent += eventHandler;
			}

			public void UnsubscribeButtonPressedEvent(ButtonPressedEventHandler eventHandler)
			{
				ButtonPressedEvent -= eventHandler;
			}
		}

		public void SetTarget(string target)
		{
			this.target = new Target(target);
		}

		private void PrintNumber(ButtonPressedEventArgs e)
		{
//			Console.Write(this.keyboard.buttons[e.CharNumber]);
			target.newData(this.keyboard.buttons[e.CharNumber]);
			counter++;
		}
	}
}
